package com.example.lewisgao.blankactivity;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * Created by LewisGao on 2015-06-26 0026.
 */
public class ListviewActivity extends AppCompatActivity implements AdapterView.OnItemClickListener,AbsListView.OnScrollListener{

    private ListView listView1;
    private ArrayAdapter<String> arrayAdapter;
    private SimpleAdapter simpleAdapter;
    private List<Map<String, Object>> dataList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.listview);

        listView1 = (ListView) findViewById(R.id.listView1);
        String[] listData = {"hello world1", "hello world2", "hello world3", "中文4", "hello world2", "hello world3", "中文4", "hello world2", "hello world3", "中文4", "hello world2", "hello world3", "中文4"};
        arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, listData);

        dataList = new ArrayList<>();
        simpleAdapter = new SimpleAdapter(this, getData(), R.layout.item, new String[]{"pic","text"}, new int[]{R.id.pic, R.id.text});
        listView1.setAdapter(simpleAdapter);
        listView1.setOnItemClickListener(this);
        listView1.setOnScrollListener(this);

    }

    private List<Map<String, Object>> getData() {
        for(int i=0;i<20;i++){
            Map<String,Object> map = new HashMap<>();
            map.put("pic",R.mipmap.ic_launcher);
            map.put("text","test"+i);
            dataList.add(map);
        }
        return dataList;
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        String text = listView1.getItemAtPosition(position)+"";
        Toast.makeText(this,"position="+position+" text="+text,Toast.LENGTH_SHORT).show();;
    }

    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {
        switch (scrollState){
            case SCROLL_STATE_FLING:
                Log.i("Main", "用户在手指离开屏幕之前，由于用力的滑了一下,视图仍然依靠惯性滑动");
                Map<String,Object> map = new HashMap<>();
                map.put("pic",R.mipmap.ic_launcher);
                map.put("text","增加项");
                dataList.add(map);
                simpleAdapter.notifyDataSetChanged();
                break;
            case SCROLL_STATE_IDLE:
                Log.i("Main","视图已经停止滑动");
                break;
            case SCROLL_STATE_TOUCH_SCROLL:
                Log.i("Main","手指没有离开屏幕，视图正在滑动");
                break;
        }
    }

    @Override
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {

    }
}
